import $ from 'jquery'

export const liepinService = {
    /*
    * 采集的信息
    *  「
    *
    *   教育经历、工作经历
    * 姓名、手机号、邮箱、性别、年龄、学历、城市、公司、职位、学校、专业、行业、标签、工作年限、期望薪资
    *
    * 」
    * */

    // 采集开始
    collection() {
        console.log('*** 猎聘 ***');
        // if (window.location.host.includes('liepin')) {
        //     // // 判断一下是否英文 todo 再说
        //     // if ($('li.active').text().search('英文简历') > -1) {
        //     //     return xxx
        //     // }
        //     return this.collection();
        // }
        let phoneImage, emailImage
        let getOCRTimer = setInterval(getOCRImage, 3000)
        if (phoneImage && emailImage) {
            clearInterval(getOCRTimer)
            getOCRTimer = null
        }

        function getOCRImage() {
            if (!phoneImage && $('img.connect-img').length === 2) {
                var phoneSrc = ''
                //  if (isNewStyle()) {
                //    //phoneSrc = $('.icon-phone').parent().find('img.connect-img').attr('src')
                // phoneSrc = $('img.connect-img').eq(0).attr('src')
                //  } else {
                //    phoneSrc = $('.resume-basic-info .telphone').attr('src')
                //  }
                phoneSrc = $('img.connect-img').eq(0).attr('src')
                if (phoneSrc) {
                    $util.toDataURL(phoneSrc, function (file) {
                        phoneImage = file
                    })
                }
            }
            if (!emailImage) {
                var eSrc = ''
                // if (isNewStyle()) {
                //   // eSrc = $('.icon-email').parent().find('img.connect-img').attr('src')
                // eSrc = $('img.connect-img').eq(1).attr('src')
                // } else {
                //   eSrc = $('.resume-basic-info .email').attr('src')
                // }
                if ($('img.connect-img').length === 2) {
                    eSrc = $('img.connect-img').eq(1).attr('src')
                } else {
                    eSrc = $('img.connect-img').eq(0).attr('src')
                }

                if (eSrc) {
                    $util.toDataURL(eSrc, function (file) {
                        emailImage = file
                    })
                }
            }
        }

        let base = getBasicInfo()
        let desired = getDesired()
        let eduction = getEducation()
        let work = getWorkInfo()
        let pro = getProInfo()
        let evaluation = getEvaluation()
        let other = getOther()
        let htmlFile = getHTML()

        // 基本信息
        function getBasicInfo() {
            //  基本信息数组
            let baseInfo = $('.basic-cont .sep-info').eq(0).html().split('<i></i>')
            let imgUrl = $(".res-logo").prop("currentSrc") // 头像
            // console.log('imgUrl@liepin', imgUrl);
            // 姓名
            let name = $('.name-box').find('h4').text().trim();
            // let name = $('.name-box .name').text().trim()
            // 在职状态
            let status = $('.name-box .user-status-tag').text().trim() || ''
            // status = $util.getWorkStateStr(status)
            // 性别
            let sex = baseInfo[0].trim()
            // 年龄
            let age = baseInfo[1].trim().replace('岁', '')
            // 工作年限
            let workExperience = baseInfo[4].trim()
            // 学历
            let qualifications = baseInfo[3].trim()
            // 当前所在城市
            let nowCity = baseInfo[2].trim()
            // 当前薪资 当前行业
            let currentSalary,currentIndustry
            // 技能标签
            let skills = ''
            $('.skill-tag').each((i, v) => {
                if (skills === '') {
                    skills = $(v).text().trim()
                } else {
                    skills += ' | ' + $(v).text().trim();
                }
            })

            $('.resume-detail-template-cont').find('.rd-info-col').each(function () {
                let title = $(this).find('.rd-info-col-title').text()
                if (title.includes('目前薪资')) {
                    currentSalary = $(this).find('.rd-info-col-cont span').text()
                } else if (title.includes('目前行业')) {
                    currentIndustry = $(this).find('.rd-info-col-cont span').text()
                }
            })


            return {
                name: name, // 姓名
                sex: sex, // 性别
                photoUrl: imgUrl, // 头像
                status: status, //在职状态
                age: age, // 年龄
                workExperience: workExperience,// 工作年限
                qualifications: qualifications,// 学历
                nowCity: nowCity,// 当前所在城市
                currentSalary: currentSalary,// 当前薪资
                currentIndustry: currentIndustry,// 当前行业
                skills: skills, // 技能标签
                maritalStatus: '',
            }
        }

        // 职业期望
        function getDesired() {

            // 行业
            let industries = $('.resume-detail-template-cont .want-job .industry').text()
            if (industries === '未填写') {
                industries = []
            } else {
                industries = industries.split('·')
            }

            // 期望职位
            let desiredJob = $('.resume-detail-template-cont .want-job .job-card-left .info .job-name').text().trim()

            // 期望地点
            let desiredCitys = $('.resume-detail-template-cont .want-job .address').text()
            // 期望年薪
            let desiredSalaryStr = $('.resume-detail-template-cont .want-job .job-card-left .info .salary').text().trim()
            let desiredSalary = desiredSalaryStr.replace(/\s/g, '')

            return {
                desiredCitys: desiredCitys, // 期望城市集合
                desiredJobName: desiredJob, // 期望职位
                desiredSalary: desiredSalary, // 期望年薪 是个范围 例如 1-2k 12薪
                industries: industries // 期望行业
            }
        }

        //  工作经历
        function getWorkInfo() {
            let work = []
            let workEls = $('.resume-detail-work-info .resume-detail-template-cont .rd-work-item-cont')
            workEls.each(function (index) {
                let detail = $(this)

                let workTime = detail.find('.rd-work-time').text().match(/\d{4}\.\d{2}\s*-\s*\d{4}\.\d{2}|\d{4}\.\d{2}\s*-\s*至今/)
                workTime = workTime ? workTime[0] : ''
                workTime = workTime.split('-')
                let workStart = workTime[0].replace('.', '-').trim()
                let workEnd = workTime[1].replace('.', '-').trim()
                let companyName = detail.find('.rd-work-info-head .rd-work-comp h5').text().trim()
                let jobName = detail.find('.rd-info-tpl-item-cont h6').text().trim()
                let subordinateCount = 0
                let area = ''
                let department = ''
                let duty = ''
                let leader = ''
                let salary = ''
                let category = ''

                detail.find('.rd-info-col').each(function () {
                    const title = $(this).find('.rd-info-col-title').text()
                    if (title.indexOf('业绩') !== -1) {
                        duty = $util.formatStrLine($(this).find('.rd-info-col-cont span').html())
                    } else if (title.indexOf('地点') !== -1) {
                        area = $(this).find('.rd-info-col-cont span').text()
                    } else if (title.indexOf('下属') !== -1) {
                        subordinateCount = $(this).find('.rd-info-col-cont span').text()
                        subordinateCount = Number(subordinateCount) || 0
                    } else if (title.indexOf('汇报') !== -1) {
                        leader = $(this).find('.rd-info-col-cont span').text()
                    } else if (title.indexOf('部门') !== -1) {
                        department = $(this).find('.rd-info-col-cont span').text()
                    }else if (title.indexOf('薪　　资') !== -1) {
                        salary = $(this).find('.rd-info-col-cont span').text()
                    }else if (title.indexOf('职位类别') !== -1) {
                        category = $(this).find('.rd-info-col-cont span').text()
                    }
                })

                let item = {
                    company: companyName, // 公司名称
                    area: area || '', // 公司所在地区
                    jobName: jobName, // 职位名称
                    category: category, // 职位类别
                    department: department, // 所在部门
                    leader: leader, // 领导-汇报对象
                    subordinateCount: subordinateCount, //  下属人数
                    startTime: workStart, // 工作时间起
                    endTime: workEnd, // 工作时间止
                    jobSalary: salary, // 薪资待遇
                    workDescription: duty, // 职责业绩
                    leavingReason: '' // 离职原因
                }

                work.push(item)
            })
            return work
        }

        //  项目经历
        function getProInfo() {
            var proInfo = []
            // 收起
            let retractLink = $('.resume-detail-project-info .resume-detail-template-cont .rd-info-other-link')
            if (retractLink.text().indexOf('收起') === -1) {
                retractLink.click()
            }
            // 采集开始
            $('.resume-detail-project-info .resume-detail-template-cont .rd-project-item-cont').each(function () {
                let data = {
                    companyName: '', // 公司名称
                    jobName: '', // 职位名称
                    name: '', // 项目名称
                    description: '', // 项目描述
                    content: '', // 项目职责
                    projectAchievement: '', //  项目业绩
                    startTime: null, // 项目开始时间
                    endTime: null, // 项目结束时间
                }

                let detail = $(this)
                data.name = detail.find('.rd-project-name').text().trim()
                let time = detail.find('.rd-project-time').text().trim()
                time = time.replace(/[（）]/g, '').split('-');
                if (time) {
                    data.startTime = time[0].replace('.', '-').trim()
                    data.endTime = time[1].replace('.', '-').trim()
                }
                detail.find('.rd-info-col').each(function () {
                    let title = $(this).find('.rd-info-col-title').text().trim()
                    if (title.includes('职务')) {
                        data.jobName = $(this).find('.rd-info-col-cont span').text().trim()
                    } else if (title.includes('公司')) {
                        data.companyName = $(this).find('.rd-info-col-cont span').text().trim()
                    } else if (title.includes('描述')) {
                        data.description = $(this).find('.rd-info-col-cont span').text().trim()
                    } else if (title.includes('职责')) {
                        data.content = $util.formatStrLine($(this).find('.rd-info-col-cont span').html())
                    } else if (title.includes('业绩')) {
                        data.projectAchievement = $util.formatStrLine($(this).find('.rd-info-col-cont span').html())
                    }
                })
                proInfo.push(data)
            })
            return proInfo
        }

        // 教育经历
        function getEducation() {
            let education = []
            $('.resume-detail-edu-info .resume-detail-template-cont .rd-edu-info-item').each(function () {
                // 学历和专业
                let detail = $(this)
                let qualifications = detail.find('.school-degree').text().trim()
                let majorName = detail.find('.school-special').text().trim()
                qualifications = $util.getEducationalAttainment(qualifications)
                let theEntranceExamination = undefined
                detail.find('.edu-school-tags .tag-item').each(function () {
                    if ($(this).text().indexOf('Fulltime') !== -1 || $(this).text().indexOf('统招') !== -1) {
                        theEntranceExamination = 1
                    }
                })
                // 校名和入学时间
                let schoolName = detail.find('.school-name').text().trim()
                let time = detail.find('.school-time').text().trim().split('-')
                let startDate = 0
                let endDate = 0
                if (time) {
                    startDate = time[0].replace('.', '-').trim()
                    endDate = time[1].replace('.', '-').trim()
                }

                let item = {
                    school: schoolName, // 学校名称
                    majorName: majorName, // 专业名称
                    startTime: startDate, // 开始时间
                    endTime: endDate, // 结束时间
                    qualifications: qualifications, // 学位
                    theEntranceExamination: theEntranceExamination, // 是否统招
                    desc: "", // 描述
                    education: $util.getEducation(qualifications)
                }
                education.push(item)
            })
            return education
        }

        // 自我评价
        function getEvaluation() {
            return $('#resume-detail-self-eva-info .resume-detail-template-cont > div').text().trim()
        }

        // 获取源html代码
        function getHTML() {
            return $util.htmlToString(document)
            // let styleStr = ''
            // $('style').each(function (i, s) {
            //     styleStr = styleStr + '<style>' + $(s).html() + '</style>'
            // })
            // let obj = $('body').clone()
            // obj.find('.H-activity-guide').remove()
            // obj.find('a').attr('href', 'javascript:;')
            // obj.find('script').remove()
            // obj.find('.c-single-resume-right').remove()
            // obj.find('.hfw-footer-wrap').remove()
            // obj.find('.hfw-header-wrap').remove()
            // obj.find('#lx2-open').remove()
            // obj.find('#lx2-close').remove()
            // obj.find('#lx2-pop').remove()
            // obj.find('img.connect-img').eq(0).attr('src', phoneImage)
            // obj.find('img.connect-img').eq(1).attr('src', emailImage)
            //
            // let html = obj.html() || 'error'
            // html = '<!DOCTYPE html>' +
            //     '<html lang="en"><head>' +
            //     // <link rel="stylesheet" href="https://concat.lietou-static.com/fe-h2-pc/v6/css/common.365c0052.css">
            //     // <link rel="stylesheet" href="https://concat.lietou-static.com/fe-h2-pc/v6/css/ResumeDetail.03eabf70.css">
            //     '<link rel="stylesheet" href="https://lietou.liex2.com/static/external_css/liepin/common.365c0052.css">' +
            //     '<link rel="stylesheet" href="https://lietou.liex2.com/static/external_css/liepin/ResumeDetail.03eabf70.css">' +
            //     styleStr +
            //     '</head><body>'
            //     + html +
            //     '</body></html>'
            // return html
        }

        // 其他 -- 语言能力、证书
        function getOther() {
            //  语言能力
            let language = $('#resume-detail-lang-info .resume-detail-template-cont .lang-name').text()
            //  附加消息
            let cert = $util.formatStrLine($('#resume-detail-addition-info .resume-detail-template-cont div').html())
            return {
                language: language,
                cert: cert
            }
        }

        return {
            href: location.href,
            base: base,
            desired: desired,
            workList: work,
            projectList: pro,
            educationList: eduction,
            other: other,
            evaluation: evaluation,
            htmlFile: htmlFile,
            htmlFileName: '猎聘_' + Date.now() + '.html',
            phoneImage: phoneImage,
            emailImage: emailImage
        }
    },
}

